<template>
  <div>
    <i-dialog
      :title="row.deviceId ? '编辑设备' : '添加设备'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
    >
      <i-form
        :items="formItems"
        :model="form"
        ref="form"
        :rules="rules"
        label-width="95px"
        class="demo-form"
      >
        <template slot="splitValue">
          <el-form-item label="分账比例" prop="splitValue">
            <div style="position: relative">
              <el-input
                placeholder="请输入分账比例（1-30%）"
                v-model="form.splitValue"
              />
              <div style="position: absolute; right: -16px; top: 0; bottom: 0">
                %
              </div>
            </div>
          </el-form-item>
        </template>
      </i-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import { addOrUpdateProfitShare } from '@/api/divideDevice'
import formValidate from '@/utils/validate'
export default {
  name: 'AddShareDevice',
  data() {
    return {
      form: {},
      formItems: [
        {
          type: 'text',
          value: 'deviceId',
          label: '设备编号',
          clearable: true,
          required: true,
        },
        {
          slot: 'splitValue',
          required: true,
        },
      ],
      rules: {
        deviceId: [
          {
            max: 32,
            message: '最多允许输入32个字符',
            trigger: ['change', 'blur'],
          },
          {
            validator: formValidate('validateExplanation'),
            trigger: ['change', 'blur'],
          },
        ],
        splitValue: [
          {
            required: true,
            message: '该项为必填项',
          },
          {
            validator: (rule, value, callback) => {
              let reg = /^[1-9]\d*$/
              if (!reg.test(value)) {
                callback(new Error('请输入正整数!'))
              } else if (value < 1 || value > 30) {
                callback(new Error('请输入大于0且小于等于30的整数！'))
              } else {
                callback()
              }
            },
            trigger: ['change', 'blur'],
          },
        ],
      },
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    row: {
      type: Object,
      default: () => {},
    },
  },
  mounted() {
    this.form = {
      deviceId: this.row.deviceId,
      splitValue: this.row.splitValue,
    }
  },
  methods: {
    outerClose() {
      this.$emit('update:visible', false)
    },
    refreshTable() {
      this.$emit('refreshTable')
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
            splitValue: parseFloat(this.form.splitValue),
            id: this.row.id, //编辑的时候需要
          }
          addOrUpdateProfitShare(params).then(() => {
            this.$message({
              message: this.row.id ? '编辑成功' : '添加成功',
              type: 'success',
            })
            this.refreshTable()
            this.outerClose()
          })
        }
      })
    },
  },
}
</script>
